package com.neuedu.wemall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.wemall.dao.entity.Brand;
import com.neuedu.wemall.dao.entity.Category;
import com.neuedu.wemall.dao.entity.ProdImg;
import com.neuedu.wemall.dao.mapper.BrandMapper;
import com.neuedu.wemall.dao.mapper.CategoryMapper;
import com.neuedu.wemall.dao.mapper.ProdImgMapper;
import com.neuedu.wemall.service.vo.ProductSearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neuedu.wemall.dao.entity.Product;
import com.neuedu.wemall.service.IProductService;
import com.neuedu.wemall.dao.mapper.ProductMapper;

import java.util.ArrayList;
import java.util.List;

@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private ProdImgMapper prodImgMapper;

    @Override
    public Product getById(Integer id) {
        Product product = getBaseMapper().selectById(id);
        if (product == null) {
            return null;
        }
        //查询一级分类
        Category sup = categoryMapper.selectById(product.getCatId1());
        product.setSup(sup);
        //查询二级分类
        Category sub = categoryMapper.selectById(product.getCatId2());
        product.setSub(sub);
        //查询品牌
        Brand brand = brandMapper.selectById(product.getBrandId());
        product.setBrand(brand);
        //查询商品图片
        QueryWrapper<ProdImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("prod_id", id);
        List<ProdImg> imgList = prodImgMapper.selectList(queryWrapper);
        product.setImgList(imgList);
        return product;
    }

    @Override
    public Page<Product> selectPageList(ProductSearchVO searchVO) {
        //打包分页查询条件
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        String keyword = searchVO.getKeyword();
        Integer minPrice = searchVO.getMinPrice();
        Integer maxPrice = searchVO.getMaxPrice();
        Integer brandId = searchVO.getBrandId();
        Integer categoryId = searchVO.getCategoryId();
        Integer subCategoryId = searchVO.getSubCategoryId();
        if (StrUtil.isNotEmpty(keyword)) {
            queryWrapper.and(qw -> {
                qw.like("title", keyword);
                qw.or();
                qw.like("tags", keyword);
                qw.or();
                qw.like("content", keyword);
            });
        }
        if (minPrice != null) {
            //ge:大于等于
            queryWrapper.ge("price", minPrice);
        }
        if (maxPrice != null) {
            //le:小于等于
            queryWrapper.le("price", maxPrice);
        }
        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);
        }
        if (categoryId != null) {
            queryWrapper.eq("cat_id1", categoryId);
        }
        if (subCategoryId != null) {
            queryWrapper.eq("cat_id2", subCategoryId);
        }
        //实例化分页对象
        Integer pageNum = searchVO.getPageNum();
        Integer pageSize = searchVO.getPageSize();
        Page<Product> page = new Page<>(pageNum, pageSize);
        //设置分页查询排序字段
        List<OrderItem> orderItems = new ArrayList<>();
        List<String> orderFields = searchVO.getOrderFields();
        List<String> orderTypes = searchVO.getOrderTypes();
        int fieldSize = orderFields != null ? orderFields.size() : 0;
        int typeSize = orderTypes != null ? orderTypes.size() : 0;
        if (fieldSize != typeSize) {
            throw new IllegalArgumentException("排序字段名称和排序方式数量不一致");
        }
        if (0 == fieldSize) {
            orderItems.add(OrderItem.asc("price"));//根据价格升序
            orderItems.add(OrderItem.desc("sold"));//根据销售量降序
            page.setOrders(orderItems);
            return getBaseMapper().selectPage(page, queryWrapper);
        }
        for (int i = 0; i < fieldSize; i++) {
            String field = orderFields.get(i);
            String type = orderTypes.get(i);
            if ("asc".equalsIgnoreCase(type)) {
                orderItems.add(OrderItem.asc(field));
            } else if ("desc".equalsIgnoreCase(type)) {
                orderItems.add(OrderItem.desc(field));
            } else {
                throw new IllegalArgumentException("排序方式必须是asc或desc");
            }
        }
        page.setOrders(orderItems);
        return getBaseMapper().selectPage(page, queryWrapper);
    }
    @Override
    public Page<Product> selectPageList(int pageNum, int pageSize, String keyword, Product product) {
       Page<Product> page=new Page<>(pageNum,pageSize);
       return getBaseMapper().selectPageList(keyword,product,page);
    }
}